#! /bin/bash
string1="请输入要保存的文件路径："
string2="不下载请输入 N/n 键退出。"
string3="请输入要保存的文件名："
string4="正在退出程序..."
string5="请输入要下载的文件链接："
string6="歌曲名已存在当前目录！"
string7="未识别的链接！"
string8="请重试！"
string9="文件查询中，请稍等..."
function wget_down {
	wget "$link" -c -O "$directory/$file_name.$1"
}
read -p "$string1" directory
while true; do
	echo "$string2"
	read -p "$string3" file_name
	if [[ "$file_name" == [nN] ]]; then
		echo "$string4"
		break
	else
		echo "$string9"
		if ! ls "$directory" | grep -q "$file_name";
		then
			read -p "$string5" link
		else
			echo "($file_name)$string6"
			continue
		fi
	fi
	if [[ "$link" == *".mp3"* ]]; then
		wget_down "mp3"
	elif [[ "$link" == *".m4a"* ]]; then
		wget_down "m4a"
	elif [[ "$link" == *".flac"* ]]; then
		wget_down "flac"
	elif [[ "$link" == *".wma"* ]]; then
		wget_down "wma"
	elif [[ "$link" == *".aac"* ]]; then
		wget_down "aac"
	elif [[ "$link" == *".ogg"* ]]; then
		wget_down "ogg"
	elif [[ "$link" == *".lrc"* ]]; then
		wget_down "lrc"
	elif [[ "$link" == *".mp4"* ]]; then
		wget_down "mp4"
	else
		echo -e "$string7\n$string8"
	fi
done
